# Filename: jit-mda-replicate.sh
# Usage: jit-mda-replicate.sh <image name> <primaryIP> <primaryPort> <backupIP> <backupPort> <size> <interval>
# Example: jit-mda-replicate.sh vm01 10.0.0.1 8002 10.0.0.2 8002 4G 200
#
# This script considers the local machine as an image provider and
# replicates the desired VM image to both the Primary and Backup hosts
# that will instantiate and provide it with failover support.
#
# Names and ports are default from Xen-Tools 4.3.1, Xen 4.1.2 and Drbd 8.3.9
#
#
#
# Parameters:
# 1) Image Name
# The virtual machine's hostname. E.g.: vm01
# All configuration and disk file names will be derived from this name,
# as per the Xen-Tools 4.3.1 defaults. E.g.: disk volumes will be called
# vm01-disk , configuration files will be /etc/xen/vm01.cfg
# This script also considers that VM disks will be on LVMs.
#
# 2 and 3) Primary IP and Port
# The IP of the Primary host. The port is currently unused, but will be
# necessary for future implementation of multiple failover protected hosts
# on a single pair of physical machines.
#
# 4 and 5) Backup IP and Port
# The IP of the Backup host. The default xend-relocation-port is 8002.
#
# 6) Size
# The size of the VM disk in megabytes or in any form allowed by the lvcreate
# command, as it is the final receptor of this parameter. For more information
# try man lvcreate .
#
# 7) Interval
# The interval between Remus checkpoints, in milisseconds. For more information
# try remus --help
#
#
#
# Pre-requisites:
# 1) All hosts must have public/private key authenticated SSH access for the root user.
# It is necessary that every host has successfully connected to each other at least once
# and that every host has successfully transferred a file via SCP to each other at least once.
# This prevents first-time confirmation messages and password requests from appearing 
# while running the script.
#
# 2) All hosts must have all jit-mda scripts on the folder /root/scripts
#
# 3) All machines must have the pv package installed. To do this, run
# sudo apt-get install pv
#

imagename=$1
primaryIP=$2
primaryPort=$3
backupIP=$4
backupPort=$5
size=$6
interval=$7

SCRIPTS_FOLDER=/root/scripts
REMOTELOG=/root/remote.log
REMOTEERR=/root/remote.err

echo `date +%H:%M:%S` Starting Script $0 $*
if [ -z "$interval" ]; then
	echo Incorrect parameters
	echo "Usage $0 <image name> <primaryIP> <primaryPort> <backupIP> <backupPort> <size> <interval>"
else
	$SCRIPTS_FOLDER/jit-mda-create-snapshot.sh $imagename $size
	
	# Manipulating Backup host
	$SCRIPTS_FOLDER/jit-mda-transfer-image.sh  $imagename $backupIP
	echo `date +%H:%M:%S` Firing remote script jit-mda-create-backup.sh on BackUp host
	echo Type ssh root@$backupIP "cat $REMOTELOG" and
	echo Type ssh root@$backupIP "cat $REMOTEERR" for information on its progress.
	ssh root@$backupIP "nohup $SCRIPTS_FOLDER/jit-mda-create-backup.sh $imagename $primaryIP $backupIP $backupPort $size $interval >$REMOTELOG 2>$REMOTEERR &"
	
	# Manipulating Primary host
	$SCRIPTS_FOLDER/jit-mda-transfer-image.sh  $imagename $primaryIP
	echo `date +%H:%M:%S` Firing remote script jit-mda-create-primary.sh on Primary host
	echo Type ssh root@$primaryIP "cat $REMOTELOG" and
	echo Type ssh root@$primaryIP "cat $REMOTEERR" for information on its progress.
	ssh root@$primaryIP "nohup $SCRIPTS_FOLDER/jit-mda-create-primary.sh $imagename $primaryIP $backupIP $backupPort $size $interval >$REMOTELOG 2>$REMOTEERR &"
fi
echo `date +%H:%M:%S` Finishing Script $0
exit 0
